﻿@model Commission.ViewModels.CustomerProduct
@using MyResources = Commission.Resources.AppResource
@using MyConfig = Commission.Helpers.Const
@using Microsoft.AspNet.Identity

@{
    ViewBag.Title = "Create";
}

@section MyJavascript {
    <script type="text/javascript">
    $(document).ready(function () {
        $('#Customer_SponsorUserName').autocomplete(
        {
            source: '@Url.Action("UserNameSearch", "Customer")'
        }
        );

        $("#Customer_SponsorUserName").blur(function () {

            var UserName = $('#Customer_SponsorUserName').val();

            $.ajax({
                url: '@Url.Action("NameFromCode", "Customer")',
                data: {
                    userName: UserName
                },
                success: function (data) {
                    $('#Customer_SponsorName').val(data);
                },
                cache: false
            });
        });

        $('#Customer_ParentUserName').autocomplete(
        {
            source: function (request, response) {
                $.ajax({
                    url: '@Url.Action("ParentCustomerSearch", "Customer")',
                    dataType: "json",
                    data: {
                        term: request.term,
                        extraParam: $("#Customer_SponsorUserName").val()
                    },
                    success: function (data) {
                        response(data);
                    }
                });
            },
            min_length: 3,
            delay: 300
        }
        );

        $("#Customer_ParentUserName").blur(function () {

            var UserName = $('#Customer_ParentUserName').val();

            $.ajax({
                url: '@Url.Action("NameFromCode", "Customer")',
                data: {
                    userName: UserName
                },
                success: function (data) {
                    $('#Customer_ParentName').val(data);
                },
                cache: false
            });
        });
    })

    function calculate(obj, rowIndex) {
        var total = 0;

        var rows = document.getElementById("mytable").getElementsByTagName("tr").length;
        var quantity = obj.value;

        var price = $("#price" + rowIndex).val();
        var amount = quantity * price;
        var totalObj = $("#total" + rowIndex);
        totalObj.val(numberWithCommas(amount));

        for (i = 0; i <= rows; i++) {
            var temp = $("#total" + i).val();
            if ("" == temp || temp == 0) continue;

            var subTotal = removeCommas($("#total" + i).val());
            total = total + subTotal;
        }

        $("#total").val(numberWithCommas(total));
    }

    function removeCommas(str) {
        if (str == null || str == "") return 0;

        return str.split(',').join('') * 1.0;
    }

    function numberWithCommas(n) {
        var parts = n.toString().split(".");
        return parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",") + (parts[1] ? "." + parts[1] : "");
    }

    </script>
}

<h2>@MyResources.LabelUpgradeCustomer</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>@MyResources.LabelMemberInfo</h4>

        @Html.ValidationSummary(true)

        <div class="form-group">
            <label class="control-label col-md-2" for="UserName">
                @MyResources.LabelUserName
            </label>

            <div class="col-md-4">
                @if (User.IsInRole("Admin"))
                {
                    @Html.TextBoxFor(model => model.Customer.UserName,
                        new { @class = "form-control", @readonly = "readonly" })
                    @Html.ValidationMessageFor(model => model.Customer.UserName)
                }
                else
                {
                    @Html.TextBoxFor(model => model.Customer.UserName, 
                        new { @class = "form-control", @readonly = "readonly" })
                    @Html.ValidationMessageFor(model => model.Customer.UserName)
                }
            </div>

            <label class="control-label col-md-2" style="width:10%" for="Gender">
                @MyResources.LabelGender
            </label>

            <div class="col-md-4" style="margin-top:6px">
                @Html.RadioButtonFor(m => m.Customer.Gender, 0,
                    new { @disabled = "disabled" }) @MyResources.LabelMale&nbsp;&nbsp;
                @Html.RadioButtonFor(m => m.Customer.Gender, 1,
                    new { @disabled = "disabled" }) @MyResources.LabelFemale
                @Html.ValidationMessageFor(model => model.Customer.Gender)
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-md-2" for="Name">
                @MyResources.LabelCustomerName
            </label>

            <div class="col-md-4">
                @Html.TextBoxFor(model => model.Customer.Name,
                    new { @class = "form-control", @readonly = "readonly" })
                @Html.ValidationMessageFor(model => model.Customer.Name)
            </div>

            <label class="control-label col-md-2" style="width:10%" for="BirthDay">
                @MyResources.LabelBirthday
            </label>

            <div class="col-md-4">
                @Html.JQueryUI().DatepickerFor(model => model.Customer.BirthDay,
                    new { @class = "form-control-date", @readonly = "readonly" })
                @Html.ValidationMessageFor(model => model.Customer.BirthDay)
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-md-2" for="IDCard">
                @MyResources.LabelCMND
            </label>

            <div class="col-md-10">
                @Html.TextBoxFor(model => model.Customer.IDCard,
                    new { @class = "form-control", @readonly = "readonly" })
                @Html.ValidationMessageFor(model => model.Customer.IDCard)
            </div>
        </div>
        
        <div class="form-group">
            <label class="control-label col-md-2" for="PhoneNumber">
                @MyResources.LabelPhoneNumber
            </label>

            <div class="col-md-10">
                @Html.TextBoxFor(model => model.Customer.PhoneNumber, 
                    new { @class = "form-control", @readonly = "readonly" })
                @Html.ValidationMessageFor(model => model.Customer.PhoneNumber)
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-md-2" for="Address">
                @MyResources.LabelAddress
            </label>

            <div class="col-md-10">
                @Html.TextBoxFor(model => model.Customer.Address,
                    new { @class = "form-control", @readonly = "readonly" })
                @Html.ValidationMessageFor(model => model.Customer.Address)
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-md-2" for="CustomerTypeID">
                @MyResources.LabelCustomerType
            </label>

            <div class="col-md-3">
                @Html.DropDownList("Customer.CustomerTypeID",
                    (IEnumerable<SelectListItem>)ViewBag.CustomerTypeIDs, string.Empty,
                    new { @class = "form-control ddlstyle" })
                @Html.ValidationMessageFor(model => model.Customer.CustomerTypeID)
            </div>

            <div class="col-md-2" style="padding-left:0px;">
                @Html.CheckBoxFor(model => model.Customer.CustomerTypeConfirmed,
                    new { @class = "form-control-checkbox" })
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-md-2" for="SponsorUserName">
                @MyResources.LabelSponsorUserName
            </label>

            <div class="col-md-2">
                @Html.TextBoxFor(model => model.Customer.SponsorUserName,
                    new { @class = "form-control", @readonly = "readonly" })
                @Html.ValidationMessageFor(model => model.Customer.SponsorUserName)
            </div>

            <div class="col-md-3" style="padding-left:0px">
                @Html.TextBoxFor(model => model.Customer.SponsorName,
                    new { @class = "form-control", @readonly = "readonly" })
            </div>

            <div class="col-md-2" style="padding-left:0px;">
                @Html.CheckBoxFor(model => model.Customer.SponsorConfirmed,
                    new { @class = "form-control-checkbox" })
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-md-2" for="ParentUserName">
                @MyResources.LabelParentUserName
            </label>
            
            <div class="col-md-2">
                @Html.TextBoxFor(model => model.Customer.ParentUserName,
                    new { @class = "form-control", @readonly = "readonly" })
                @Html.ValidationMessageFor(model => model.Customer.ParentUserName)

            </div>

            <div class="col-md-3" style="padding-left:0px">
                @Html.TextBoxFor(model => model.Customer.ParentName,
                    new { @class = "form-control", @readonly="readonly" })
            </div>

            <div class="col-md-2" style="padding-left:0px;">
                @Html.CheckBoxFor(model => model.Customer.ParentConfirmed,
                    new { @class = "form-control-checkbox" })
            </div>
        </div>

        <h4>@MyResources.LabelProductInfo</h4>

        <p>
            @Html.ValidationMessageFor(model => model.Products)
        </p>

        <table id="mytable" class="table">
            <tr>
                <th style="width:100px">
                    @MyResources.LabelProductNo
                </th>
                <th style="width:180px">
                    @MyResources.LabelProductName
                </th>
                <th style="text-align:left; width:120px">
                    @MyResources.LabelProductImage
                </th>
                
                <th style="width:100px">
                    @MyResources.LabelUnit
                </th>

                <th style="text-align:center; width:80px">
                    @MyResources.LabelPPPoint
                </th>

                <th style="text-align:center; width:120px">
                    @MyResources.LabelKHPrice
                </th>

                <th style="width:90px">
                    @MyResources.LabelQuantity
                </th>

                <th class="text-center" style="width:160px">
                    @MyResources.LabelTotal
                </th>

                <th class="text-center">
                    @MyResources.LabelNote
                </th>
            </tr>

            @for (int i = 0; i < Model.Products.Count; i++)
            {
                <tr>

                    <td style="padding-top:14px">
                        @Html.DisplayFor(modelItem => Model.Products[i].ProductNo)
                    </td>
                    <td style="padding-top:14px">
                        @Html.DisplayFor(modelItem => Model.Products[i].ProductName)
                    </td>

                    <td>
                        @if (Model.Products[i].FileId != null && Model.Products[i].FileId != 0)
                        {
                            <img src="@Url.Content(Model.Products[i].FileUrl)" height="100" width="100" alt="Image" />
                        }
                    </td>
                   
                    <td class="text-center" style="padding-top:14px">
                        @Html.DisplayFor(modelItem => Model.Products[i].UnitName)
                    </td>

                    <td class="text-right" style="padding-top:14px">
                        @Html.DisplayFor(modelItem => Model.Products[i].PPPoint)
                    </td>

                    <td class="text-right" style="padding-top:14px">
                        @Html.Raw(string.Format("{0:0,0}", Model.Products[i].Price))
                    </td>

                    <td>
                        @Html.TextBoxFor(modelItem => Model.Products[i].Quantity,
                         new
                         {
                             @class = "text-right form-control",
                             @onchange = "calculate(this, " + i + ")",
                             @id = "quantity" + i
                         })
                    </td>

                    <td>
                        @Html.TextBoxFor(modelItem => Model.Products[i].DisplayAmount,
                        new
                        {
                            @class = "text-right form-control",
                            @readonly = "readonly",
                            @id = "total" + i
                        })
                    </td>

                    <td style="padding-top:14px">
                        @Html.DisplayFor(modelItem => Model.Products[i].Note)
                    </td>

                    @Html.Hidden("row", i)
                    @Html.HiddenFor(modelItem => Model.Products[i].ProductId)
                    @Html.HiddenFor(modelItem => Model.Products[i].ProductNo)
                    @Html.HiddenFor(modelItem => Model.Products[i].ProductName)
                    @Html.HiddenFor(modelItem => Model.Products[i].Price, new { @id = "price" + i })
                    @Html.HiddenFor(modelItem => Model.Products[i].UnitId)
                    @Html.HiddenFor(modelItem => Model.Products[i].UnitName)
                    @Html.HiddenFor(modelItem => Model.Products[i].PPPoint)
                    @Html.HiddenFor(modelItem => Model.Products[i].FileId)
                    @Html.HiddenFor(modelItem => Model.Products[i].File.File)
                </tr>
            }

            <tr>
                <th style="width:100px">
                </th>
                <th style="width:180px">
                </th>
                <th style="text-align:left; width:120px">
                </th>

                <th style="width:100px">
                </th>

                <th style="text-align:center; width:80px">
                </th>

                <th style="text-align:center; width:120px">
                </th>

                <th style="width:90px">
                </th>

                <th class="text-center" style="width:100px">
                    @Html.TextBox("total", "",
                        new
                        {
                            @class = "text-right form-control",
                            @readonly = "readonly",
                            @id = "total"
                        })
                </th>

                <th class="text-center">
                </th>
            </tr>
        </table>

        <hr />
  
        <p style="width:100%;">
            <input type="submit" id="btnPurchase"
                   value="@MyResources.LabelUpgrade" class="btn btn-default"
                   style="margin-left:17%; width:90px;" />
        </p>

        @Html.HiddenFor(t => t.Customer.CustomerID)
        @Html.HiddenFor(t => t.Customer.DirectCustomerID)
        @Html.HiddenFor(t => t.Customer.OldCustomerTypeID)
        @Html.HiddenFor(t => t.Customer.SponsorCustomerID)
        @Html.HiddenFor(t => t.Customer.ParentCustomerID)
        @Html.HiddenFor(t => t.Customer.Position)
        @Html.HiddenFor(t => t.Customer.UpgradeDate)
        @Html.HiddenFor(t => t.Customer.AttendantDate)
        @Html.HiddenFor(t => t.Customer.ValidDate)
    </div>
}

<div>
    @Html.ActionLink(@MyResources.LinkBackToList, "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
